package com.zhongwang.mms.module.basic.service;

import com.zhongwang.mms.gen.entity.*;
import com.zhongwang.mms.gen.mapper.TDeptMapper;
import com.zhongwang.mms.module.basic.dao.TDeptDao;
import com.zhongwang.mms.module.basic.model.TDeptModel;
import com.zhongwang.mms.module.system.log.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.zhongwang.mms.constant.Constant.STATUS_NORMAL;


/**
 * @Description: 部门 服务层
 * @Author: China.wsg
 * @Date: 2020/04/13 10:23
 */
@Service
public class TDeptService {

    @Autowired
    private TDeptDao deptDao;
    @Autowired
    private LogService logService;
    @Autowired
    private TDeptDao tDeptDao;

    /**
     * 保存部门信息
     *
     * @param dept 部门信息
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveDept(TDept dept) {
        dept.setDepStatus(1);
        dept.setDepCreateTime(new Date());
        int row = deptDao.insert(dept);
        return row;
    }

    /**
     * 根据主键查询部门
     *
     * @param id 主键
     * @return 部门
     */
    public TDept findDeptById(Integer id) {
        return deptDao.selectByPrimaryKey(id);
    }

    /**
     * 修改部门
     *
     * @param dept 部门信息
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateDept(TDept dept) {
        int row = deptDao.updateByPrimaryKeySelective(dept);
        return row;
    }

    /**
     * 取得车间
     *
     * @return
     */
    public List<TDept> findDeptInfo(TDept dept) {

        TDeptExample ex = new TDeptExample();
        ex.setOrderByClause("dep_order_no asc");
        TDeptExample.Criteria criteria = ex.createCriteria();
        criteria.andDepStatusEqualTo(1);  // 状态 0：删除 1：正常

        if (dept.getDepName() != null && !"".equals(dept.getDepName())) {
            criteria.andDepNameLike("%" + dept.getDepName() + "%");
        }

        return deptDao.selectByExample(ex);
    }

    /**
     * 按名称名查询部门
     *
     * @param name 部门名
     * @return 部门信息
     */
    public TDept findDeptByName(String name) {
        TDeptExample ex = new TDeptExample();
        TDeptExample.Criteria criteria = ex.createCriteria();
        criteria.andDepNameEqualTo(name);
        criteria.andDepStatusNotEqualTo(0);

        List<TDept> depts = deptDao.selectByExample(ex);
        if (depts != null && depts.size() > 0) {
            return depts.get(0);
        }
        return null;
    }

    /**
     * 修改部门
     *
     * @param dept 部门信息
     * @return 修改行数
     */
    public int delDept(TDept dept) {
        int row = deptDao.updateByPrimaryKeySelective(dept);

        // 删除下级
        TDept tDept = new TDept();
        tDept.setDepStatus(0);
        TDeptExample ex = new TDeptExample();
        TDeptExample.Criteria criteria = ex.createCriteria();
        criteria.andDepPidEqualTo(dept.getDepId());
        deptDao.updateByExampleSelective(tDept, ex);

        return row;
    }

    public List<TDeptModel> findDeptCheckedByUserId(Integer id) {
        List<TDeptModel> all = deptDao.selectByTDeptModel(new TDeptModel());
        TDeptModel dept = deptDao.findByUserId(id);
        if (null != dept) {
            for (TDeptModel o : all) {
                if (o.getId().equals(dept.getId())) {
                    o.setCheckFlag(true);
                    break;
                }
            }
        }
        return all;
    }
}
